package com.culture.bootdemo.utils.fileutils;

import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.text.csv.CsvReader;
import cn.hutool.core.text.csv.CsvUtil;
import cn.hutool.core.text.csv.CsvWriter;
import cn.hutool.core.util.CharsetUtil;
import com.alibaba.fastjson.JSON;
import com.culture.bootdemo.model.vo.CSVDemoData;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: wenjl
 * @Date: 2021/3/15 16:29
 * @Description: hutool工具  csv文件生成和读取
 */
@Slf4j
public class CSVUtils {

    private final static String path = "E:\\csvfile\\";

    public static void read() {
        String fileName = "testDemo1.csv";
        CsvReader reader = CsvUtil.getReader();
        List<CSVDemoData> result = reader.read(ResourceUtil.getUtf8Reader(path + fileName), CSVDemoData.class);
        log.info("读取结果为:{}", JSON.toJSONString(result));
    }


    public static void write() {
        String fileName = "testDemo1" + ".csv";
        CsvWriter writer = CsvUtil.getWriter(path + fileName, CharsetUtil.CHARSET_UTF_8);
        List<String> list = new ArrayList<>();
        list.add("id,name,userId");
        list.add("1,zhangsan1,10");
        list.add("2,zhangsan2,20");

        writer.write(list);
    }


    public static void main(String[] args) {
        read();
    }


}
